package com.fanqwz.hexieshe.data.entity.wp.mapper;

import com.fanqwz.hexieshe.data.entity.wp.Taxonomy;
import com.fanqwz.hexieshe.domain.Article;
import com.fanqwz.hexieshe.data.entity.wp.Author;
import com.fanqwz.hexieshe.data.entity.wp.Post;
import com.fanqwz.hexieshe.domain.Category;
import com.fanqwz.hexieshe.domain.Comment;
import com.fanqwz.hexieshe.domain.HeadLine;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * Created by fanqw on 2017/6/30.
 */

@Singleton
public class EntityMapper {

    @Inject
    EntityMapper() {

    }

    public Article transform(Post post) {
        if (post != null) {
            Article article = new Article();
            article.setID(post.getID());
            article.setAuthor(transform(post.getAuthor()));
            article.setContent(post.getContent());
            article.setDate_gmt(post.getDate_gmt());
            article.setDate_tz(post.getDate_tz());
            article.setFeatured_image(post.getFeatured_image() == null ? null : post.getFeatured_image().getSource());
            article.setModified_gmt(post.getModified_gmt());
            article.setModified_tz(post.getModified_tz());
            article.setTitle(post.getTitle());
            article.setType(post.getType());
            article.setExcerpt(post.getExcerpt());
            return article;
        }

        return null;
    }

    public HeadLine transform(Article article) {
        HeadLine headLine = new HeadLine();
        headLine.setTitle(article.getTitle());
        headLine.setId(article.getID());

        org.jsoup.nodes.Document document = Jsoup.parse(article.getExcerpt());
        Elements elements = document.select("p");

        if (elements.get(0) != null) { //image
            Elements imgs = elements.get(0).getElementsByTag("img");

            if (imgs.size() > 0) {
                headLine.setImageUrl(imgs.get(0).attr("src"));
            }
        }

//        if (elements.get(1) != null) { //prief
//            headLine.setContent(elements.get(1).toString());
//        }

        for (int i = 0; i < elements.size(); i++) { // prief
            if (elements.get(i).text().length() > 0) {
                Element e = elements.get(i);
                Elements childs = e.children();

                for (int j = 0; j < childs.size(); j++) {
                    if ("img".equals(childs.get(j).tagName()))
                        childs.get(j).remove();
                }

                headLine.setContent(e.html());
                break;
            }
        }


        if (headLine.getImageUrl() == null)
            headLine.setImageUrl(article.getFeatured_image());

        if (headLine.getContent() == null)
            headLine.setContent(article.getContent());

        return headLine;
    }

    public List<HeadLine> transformHeadline(List<Post> list) {
        if (list != null) {
            List<HeadLine> articles = new ArrayList<>(list.size());
            for (Post post : list) {
                articles.add(transform(transform(post)));
            }
            return articles;
        }

        return null;
    }

    public com.fanqwz.hexieshe.domain.Author transform(Author author) {
        if (author != null) {
            com.fanqwz.hexieshe.domain.Author a = new com.fanqwz.hexieshe.domain.Author();
            a.setAvatar(author.getAvatar());
            a.setDescription(author.getDescription());
            a.setFirst_name(author.getFirst_name());
            a.setID(author.getID());
            a.setLast_name(author.getLast_name());
            a.setName(author.getName());
            a.setNickname(author.getNickname());
            a.setRegisterd(author.getRegisterd());
            a.setURL(author.getURL());
            a.setUsername(author.getUsername());
            return a;
        }

        return null;
    }

    public List<Category> transformCategory(List<Taxonomy> taxonomies) {
        if (taxonomies != null) {
            List<Category> categories = new ArrayList<>(taxonomies.size());

            for (Taxonomy taxonomy : taxonomies) {
                Category category = new Category();
                category.setId(taxonomy.getId());
                category.setName(taxonomy.getSlug());
                category.setDisplayName(taxonomy.getName());
                categories.add(category);
            }

            return categories;
        }

        return null;
    }

    public Comment transform(com.fanqwz.hexieshe.data.entity.wp.Comment comment) {
        if (comment != null) {
            Comment c = new Comment();
            c.setID(comment.getID());
            c.setArticleID(comment.getPost());
            c.setContent(comment.getContent());
            c.setAuthor(transform(comment.getAuthor()));
            return c;
        }

        return null;
    }

    public List<Comment> transformComments(List<com.fanqwz.hexieshe.data.entity.wp.Comment> comments) {
        List<Comment> commentList = new ArrayList<>(comments.size());

        for (com.fanqwz.hexieshe.data.entity.wp.Comment c : comments) {
            commentList.add(transform(c));
        }

        return commentList;
    }
}
